<template>
  <div class="position-warp">
    <a-row>
      <a-col :span="24">
        <div class="position-table-warp">
          <div class="table-search">
            <a-form :model="state.tableForm" @submit="handleSubmit">
              <a-row>
                <a-col :span="7">
                  <a-form-item field="name" label="职位名称">
                    <a-input
                      v-model="state.tableForm.name"
                      placeholder="请输入职位名称"
                    /> </a-form-item
                ></a-col>

                <a-col :span="3">
                  <a-form-item style="justify-content: flex-end">
                    <a-button @click="handleReset"> 重置 </a-button>
                    <a-button
                      @click="handleSearch"
                      style="margin-left: 5px"
                      type="primary"
                    >
                      查询
                    </a-button>
                  </a-form-item>
                </a-col>
              </a-row>
            </a-form>
          </div>
          <div class="table-actions">
            <a-button
              type="outline"
              status="danger"
              v-if="selectedKeys.length > 0"
              @click="handleDelete"
              >删除</a-button
            >
            <a-button
              type="primary"
              @click="handleCreate"
              style="margin-left: 5px"
              >新增</a-button
            >
          </div>
          <a-table
            row-key="code"
            :columns="columns"
            :data="state.tableData"
            :row-selection="rowSelection"
            v-model:selectedKeys="selectedKeys"
            :pagination="false"
          >
            <template #optional="{ record }">
              <a-button @click="handleUpdate(record)">编辑</a-button>
            </template>
          </a-table>
          <div class="pagetion">
            <a-pagination
              :total="state.tableTotal"
              :page-size="state.pageInfo.pageSize"
              @change="pageChange"
              @page-size-change="pageSzChange"
              show-page-size
            />
          </div>
        </div>
      </a-col>
    </a-row>

    <EditForm ref="editFormRef" @refresh="getTableData()"></EditForm>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from "vue";
import EditForm from "./editForm.vue";
import Modal from "@arco-design/web-vue/es/modal";
import { usePositionDeleteApi, usePositionPageApi } from "@/api";
import { Message } from "@arco-design/web-vue";
const editFormRef = ref();
// 表格Form
const useTableForm = () => {
  return {
    code: "",
    name: "",
    posCode: "",
  };
};
const state = reactive({
  tableForm: useTableForm(),
  tableTotal: 0,
  tableData: [],
  pageInfo: {
    pageNum: 1,
    pageSize: 10,
  },
  modelVisible: false, //控制表单弹窗
  deleteModelVisible: false, //控制删除弹窗
});

onMounted(() => {
  getTableData();
});
const pageChange = (c) => {
  state.pageInfo.pageNum = c;
};
const pageSzChange = (p) => {
  state.pageInfo.pageSize = p;
};

// 表格搜索重置
const handleReset = () => {
  state.tableForm = useTableForm();
  state.pageInfo.pageNum = 1;
  state.pageInfo.pageSize = 10;
  getTableData();
};
// 表格搜索查询
const handleSearch = () => {
  getTableData();
};

//获取表格数据
const getTableData = () => {
  usePositionPageApi(Object.assign({}, state.pageInfo, state.tableForm)).then(
    ({ data: { list, total } }) => {
      state.tableTotal = total;
      state.tableData = list;
    }
  );
};

const handleCreate = () => {
  editFormRef.value.open("create");
};
const handleUpdate = (row: any) => {
  editFormRef.value.open("update", row);
};
const handleDelete = () => {
  Modal.warning({
    title: "删除",
    content: "是否确认删除选中的职位？",
    modalStyle: { textAlign: "center", fontSize: "16px" },
    bodyStyle: { fontSize: "14px" },
    cancelText: "取消",
    width: 300,
    closable: true,
    onOk: () => {
      usePositionDeleteApi({ codeList: selectedKeys.value })
        .then(() => {
          Message.success("操作成功");
        })
        .catch(() => Message.error("操作失败"))
        .finally(() => {
          selectedKeys.value = [];
          getTableData();
        });
    },
  });
};

const handleModel = () => {};

const handleSubmit = () => {
  console.log("11");
};

const rowSelection = reactive({
  type: "checkbox",
  showCheckedAll: false,
  onlyCurrent: false,
});

const selectedKeys = ref([]);

const columns = [
  {
    title: "名称",
    dataIndex: "name",
    align: "center",
  },
  {
    title: "操作",
    align: "center",
    slotName: "optional",
  },
];
</script>

<style scoped lang="less">
.position-warp {
  .position-tree-warp {
    border-right: 1px solid #e8e8e8;
    height: calc(100vh - 110px);
    overflow-y: hidden;
    padding: 10px;

    .tree-search {
      height: 52px;
    }
  }
  .position-table-warp {
    padding: 10px;

    .table-search {
      :deep(.arco-form-item) {
        margin-bottom: 0 !important;
      }
      :deep(.arco-form-item-content-flex) {
        justify-content: flex-end !important;
      }
      margin-bottom: 5px;
    }
    .table-actions {
      display: flex;
      justify-content: flex-end;
      margin-bottom: 5px;
    }
  }
  .pagetion {
    margin-top: 10px;
    display: flex;
    justify-content: flex-end;
  }
}
</style>
